package com.zjw.contorller;

import com.zjw.domain.dto.EditPasswordDto;
import com.zjw.domain.dto.EditUserDto;
import com.zjw.domain.vo.ResponseResult;
import com.zjw.service.UserService;
import com.zjw.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUsernameAvatar")
    public ResponseResult getUsernameAvatar(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long jwtId = JWTUtil.getJWTId(token);

        return userService.getUsernameAvatar(jwtId);
    }

    @GetMapping("/userInfo")
    public ResponseResult getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long jwtId = JWTUtil.getJWTId(token);
//        查询当前用户id并返回
        return userService.getUserInfo(jwtId);
    }


    @PutMapping("/editUser")
    public ResponseResult editUser(HttpServletRequest request, @RequestBody EditUserDto editUserDto) {
        String token = request.getHeader("token");
        Long jwtId = JWTUtil.getJWTId(token);

        return userService.editUser(jwtId, editUserDto);
    }

    @PutMapping("/editPassword")
    public ResponseResult editPassword(HttpServletRequest request, @RequestBody EditPasswordDto editPasswordDto) {
        String token = request.getHeader("token");
        Long jwtId = JWTUtil.getJWTId(token);

        return userService.editPassword(jwtId, editPasswordDto);
    }

    @GetMapping("/getUserId")
    public ResponseResult getUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long jwtId = JWTUtil.getJWTId(token);
        return ResponseResult.ok(jwtId);
    }
}
